﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace MediaCenter
{
    public partial class Options : Form
    {
        private string path = "pathFilmer";
        private string value;

        public Options()
        {
            InitializeComponent();
            foreach (string s in showPaths())
            {
                listBox1.Items.Add(s);
            }
        }

        private string[] showPaths()
        {
            var x = getXMLData("pathFilmer");
            var y = getXMLData("pathSerier");
            var z = getXMLData("pathMusikk");
            string[] paths = new string[x.Length + y.Length + z.Length];
            x.CopyTo(paths, 0);
            y.CopyTo(paths, x.Length);
            z.CopyTo(paths, x.Length + y.Length);
            
            return paths;
        }

        private string[] getXMLData(string input)
        {
            string[] temp = ConfigurationManager.AppSettings.GetValues(input);

            var split = temp[0].Split(',');
            int i = 0;
            var type = new string[split.Length];
            foreach (string p in split)
            {
                Console.WriteLine(p);
                type[i] = split[i];
                i++;
            }
            return type;
        }

        private void Options_Load(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text = "X:\\Share\\";
            panel1.Visible = false;
            button2.Enabled = true;
            button3.Enabled = true;
            button4.Enabled = true;
        }

        private void btn_AddNew_Click(object sender, EventArgs e)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            string[] temp = getXMLData(path);

            for (int j = 0; j < temp.Length; j++)
            {
                value += "," + temp[j];
            }

            config.AppSettings.Settings[path].Value = value + "," + textBox1.Text;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");

            panel1.Visible = false;
            textBox1.Text = "X:\\Share\\";
            button2.Enabled = true;
            button3.Enabled = true;
            button4.Enabled = true;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            path = "pathFilmer";
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            path = "pathSerier";
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            path = "pathMusikk";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this, "Not Implemented", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
